package com.example.runningball;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.runningball.figures.Corner;

public class ShapeTest extends Activity {
	private class RenderView extends View {
		Paint paint;
		Corner rectLeftUpperCorner = new Corner(100, 100, 5);
		Corner rectRightLowerCorner = new Corner(200, 200, 5);
		float degrees = 0.0f;
		int inc = 1;
		Random random = new Random();
		Bitmap image;

		@SuppressWarnings("all")
		public RenderView(Context context) {
			super(context);
			PowerManager powerManager =
					(PowerManager)context.getSystemService(Context.POWER_SERVICE);
					WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
			paint = new Paint();
			try {
				AssetManager assetManager = context.getAssets();
				InputStream inputStream = assetManager.open("droid_ball.png");

				image = BitmapFactory.decodeStream(inputStream);
				inputStream.close();
			} catch (IOException e) {
			}
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawRGB(255, 255, 255);
			
			if (random.nextInt(3) == 0) {
				inc *= -1;
			}
			degrees += inc;
			canvas.rotate(degrees, canvas.getWidth() / 2, canvas.getHeight() / 2);	
			drawLine(canvas);			
			drawCircle(canvas);			
			drawMovableRect(canvas);
			drawImage(canvas);
			invalidate();
			canvas.restore();			
		}
		
		private void drawImage(Canvas canvas) {
			Rect dst = new Rect();
			dst.set(50, 50, 350, 350);
			Paint paint = new Paint();	
			canvas.drawBitmap(image, canvas.getWidth() / 2 - 120, canvas.getHeight() / 2 - 120, null);			
		}

		private void drawMovableRect(Canvas canvas) {
			paint.setStyle(Style.FILL);
			paint.setColor(0x770000ff);
			canvas.drawRect(rectLeftUpperCorner.x, rectLeftUpperCorner.y, 
					rectRightLowerCorner.x, rectRightLowerCorner.y, paint);
			moveRectLeftUp();
			
			if (rectRightLowerCorner.isOutOfScreenUp()) {
				moveRectToScreenDown(canvas);
			}
							
		}
		
		private void moveRectLeftUp() {
			rectLeftUpperCorner.moveUp();
			rectLeftUpperCorner.moveLeft();
			
			rectRightLowerCorner.moveUp();
			rectRightLowerCorner.moveLeft();
		}
		
		private void moveRectToScreenDown(Canvas canvas) {
			rectLeftUpperCorner.x = canvas.getWidth() - 1;
			rectLeftUpperCorner.y = canvas.getHeight() - 1;
			
			rectRightLowerCorner.x = rectLeftUpperCorner.x + 100;
			rectRightLowerCorner.y = rectLeftUpperCorner.y + 100;
		}

		private void drawCircle(Canvas canvas) {
			paint.setStyle(Style.FILL_AND_STROKE);
			paint.setColor(0xff00ff00);
			canvas.drawCircle(canvas.getWidth() / 2 - 120, canvas.getHeight() / 2 - 120, 40, paint);
		}

		private void drawLine(Canvas canvas) {
			paint.setColor(Color.RED);
			canvas.drawLine(0, 0, canvas.getWidth() - 1, canvas.getHeight() - 1, paint);
		}		
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setFullScreen();
		setContentView(new RenderView(this));
	}
	
	public void setFullScreen() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);			
	}
}
